#!/usr/bin/env python
# encoding: utf-8
"""
topic.py

Created by 姚 远 on 2009-07-09.
Copyright (c) 2009 __MyCompanyName__. All rights reserved.
"""
import wsgiref.handlers
import email
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp \
	import template
	
class Msg(db.Model):
	when = db.DateTimeProperty(auto_now_add=True)
	who=db.StringProperty()
	cnts=db.TextProperty()
	
class MainHandler(webapp.RequestHandler):
	def get(self):
		msg = db.GqlQuery('SELECT * FROM Msg')
		values = {
			'msg':msg
		}
		self.response.out.write(template.render('msg.html', values))

	def post(self):
		sender = self.request.get("from", "")
		recipient = self.request.get("to", "")
		message = email.message_from_string(str(self.request.body))
		messag = Msg(who=sender,cnts=str(message))
		messag.put()


def main():
	  mssg = webapp.WSGIApplication([('/msg\.html', MainHandler)],
	                                       debug=True)
	  wsgiref.handlers.CGIHandler().run(mssg)

if __name__ == '__main__':
	main()